package secondStage.part3.Collecton集合.Collection迭代器遍历运用;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @ClassName: [CollectionTest]
 * @Description: Collection集合存储学生对象并遍历
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-02]
 * @Version: [v1.0]
 */
//需求:创建一个存储学生对象的集合。存储3个学生对象，使用程序实现在控制台遍历该集合
public class CollectionTest {
    public static void main(String[] args) {
        //创建集合对象
        Collection<Student> c = new ArrayList<Student>();
        //创建学生类对象
        Student s1 = new Student("黄先生", 14);
        Student s2 = new Student("六先生", 314);
        Student s3 = new Student("四先生", 54);
        //添加元素到集合
        c.add(s1);
        c.add(s2);
        c.add(s3);
        //遍历集合（迭代器的方式)
        Iterator<Student> it = c.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}
